package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.qo.BidRequestQueryObject;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ExpBidRequestController {

    @Autowired
    private IBidRequestService bidRequestService;

    /**
     * 体验标发布界面
     * @param model
     * @return
     */
    @RequestMapping("/expBidRequestPublishPage")
    public String expBidRequestPublishPage(Model model){
        model.addAttribute("minBidRequestAmount", Constants.BORROW_AMOUNT_MIN);
        model.addAttribute("minBidAmount", Constants.BID_AMOUNT_MIN);
        return "expbidrequest/expbidrequestpublish";
    }

    /**
     * 发布体验标
     * @param model
     * @return
     */
    @RequestMapping("/expBidRequestPublish")
    @ResponseBody
    public JSONResult expBidRequestPublish(BidRequest bidRequest){
        JSONResult result = null;
        try {
            bidRequestService.expApply(bidRequest);
            result = new JSONResult("");
        } catch (DisplayableException ex) {
            //异常需要在前台显示
            ex.printStackTrace();
            result = new JSONResult(false, ex.getMessage());
        } catch (Exception e){
            //异常记录到数据库或日志文件中
            e.printStackTrace();
            result = new JSONResult(false, "系统异常,请联系管理员");
        }
        return result;
    }
    /**
     * 体验标管理列表
     * @param model
     * @return
     */
    @RequestMapping("/expBidRequest_list")
    public String expBidRequestList(@ModelAttribute("qo") BidRequestQueryObject qo, Model model){
        qo.setBidRequestType(Constants.BIDREQUEST_TYPE_EXP);
        model.addAttribute("pageInfo", bidRequestService.queryPage(qo));
        return "expbidrequest/expbidrequestlist";
    }

}
